import {Component, OnInit, ViewChild} from '@angular/core';
import {Router} from '@angular/router';
import {ModalDirective} from 'ngx-bootstrap';
import {PermissionService} from '../../../lib/service/admin/permission.service';
import {PermissionList, AddPermission, EditPermission, PermissionInfo} from './permission';

@Component({
    selector: 'app-permission',
    templateUrl: './permission.component.html',
    styleUrls: ['./permission.component.scss'],
    providers: [PermissionService]
})
export class PermissionComponent implements OnInit {

    totalItems: number;
    currentPage = 1;
    itemsPerPage = 12;
    name: string;
    model: string;
    orderField: string;
    orderSort: string;
    type: string;
    message: string;
    isShow: boolean = false;
    permissionList = new PermissionList();
    permissionInfo = new PermissionInfo();
    addPermission = new AddPermission();
    editPermission = new EditPermission();

    @ViewChild('addModal') public addModal: ModalDirective;
    @ViewChild('editModal') public editModal: ModalDirective;

    constructor(
        private router: Router,
        private permissionService: PermissionService
    ) {
        this.name = '';
        this.model = '';
        this.orderField = '';
        this.orderSort = '';
    }

    ngOnInit() {
        this.getPermissionList();
    }

    getPermissionList() {
        this.permissionService.getPermissionList(this.name, this.model, this.orderField, this.orderSort, this.currentPage, this.itemsPerPage).subscribe(
            data => {
                if(data.code === 2000) {
                    this.permissionList.list = data.data.data;
                    //this.currentPage = data.data.currentPage;
                    this.totalItems = data.data.total;
                }
            }
        );
    }

    getPermissionInfo(id) {
        this.permissionService.getPermissionInfo(id).subscribe(
            data => {
                if(data.code === 2000) {
                    this.permissionInfo = data.data
                }
            }
        );
    }

    addPermissionSubmit() {
        this.permissionService.addPermission(this.addPermission).subscribe(
            data => {
                if(data.code === 2000) {
                    this.type = 'success';
                    this.message = '添加成功';
                    this.isShow = true;
                    this.addModal.hide();
                    this.getPermissionList();
                }
            }
        );
    }

    editPermissionSubmit() {
        this.editPermission.id = this.permissionInfo.id;
        this.editPermission.name = this.permissionInfo.name;
        this.editPermission.slug = this.permissionInfo.slug;
        this.editPermission.description = this.permissionInfo.description;
        this.editPermission.model = this.permissionInfo.model;
        this.editPermission.orderby = this.permissionInfo.orderby;
        this.permissionService.editPermission(this.editPermission).subscribe(
            data => {
                if(data.code === 2000) {
                    this.type = 'success';
                    this.message = '修改成功';
                    this.isShow = true;
                    this.editModal.hide();
                    this.getPermissionList();
                }
            }
        );
    }

    delPermission(id) {
        this.permissionService.delPermission(id).subscribe(
            data => {
                if(data.code === 2000) {
                    this.type = 'success';
                    this.message = '删除成功';
                    this.isShow = true;
                    this.getPermissionList();
                }
            }
        );
    }


    /*pageChanged(event: any): void {

    }*/

    pageChanged(event: any): void{
      window.scrollTo(0,0);
      this.currentPage = event.page;
      this.getPermissionList();
      //this.getTemplateTypeList(event.page,this.searchData)
    }

    promptIsShow(event: any): void{
        this.isShow = event;
    }
}
